package com.ksr.logistics.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.AnonymousFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyAnonymousFilter extends AnonymousFilter {
    /**
     * Always returns <code>true</code> allowing unchecked access to the underlying path or resource.
     *
     * @param request
     * @param response
     * @param mappedValue
     * @return <code>true</code> always, allowing unchecked access to the underlying path or resource.
     */
    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()){
            HttpServletRequest req = (HttpServletRequest) request;
            if ("/logistics/login.jsp".equals(req.getRequestURI())){
                HttpServletResponse resp=(HttpServletResponse)response;
                try {
                    resp.sendRedirect("/logistics/index/main");
                } catch (Exception e){
                }
            }
        }
        return super.onPreHandle(request, response, mappedValue);
    }


}
